package br.com.merks.controller;

import java.util.List;

import org.apache.log4j.Logger;

import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;
import br.com.caelum.vraptor.view.Results;
import br.com.merks.Util;
import br.com.merks.controller.component.MarketSessionHandler;
import br.com.merks.controller.component.SecurityModule;
import br.com.merks.controller.vo.MarketInfoVO;
import br.com.merks.model.Category;
import br.com.merks.model.Market;
import br.com.merks.model.MarketAnswer;
import br.com.merks.model.MarketComment;
import br.com.merks.model.StatusType;
import br.com.merks.service.CategoryService;
import br.com.merks.service.MarketService;
import br.com.merks.service.TradeService;
import br.com.yx.vraptor.security.Protect;

@Resource
public class MarketController {
	private Validator validator = null;
	private MarketService questionService = null;
	private Result result = null;
	private MarketSessionHandler questionSessionHandler;
	private CategoryService categoryService;
	private TradeService tradeService = null;
	private SecurityModule loginHandler;
	public static final Logger logger = Logger.getLogger(MarketController.class);
	
	public MarketController(TradeService tradeService, MarketService questionService, Result result, Validator validator, MarketSessionHandler questionSessionHandler, CategoryService categoryService, SecurityModule loginHandler) {
	    this.validator = validator;
	    this.questionService = questionService;
	    this.result = result;
	    this.questionSessionHandler = questionSessionHandler;
	    this.categoryService = categoryService;
		this.tradeService = tradeService;
		this.loginHandler = loginHandler;
	   
}
	@Path(priority = 1, value = "/market/list")
	public List<Market> list() {
		return questionService.list();
	}
	public void data(String curr) {
		
	}
	@Path(priority = 1, value = "/market/form")
	public void form() {
		this.result.include("categories",this.categoryService.list());
	}
	
	
	public void error() {
		
	}
	/* Ajax Handlers */
	@Post
	
	@Path(priority = 1, value = "/market/add")
	public Market add(Market market) {
		/* Validando a entrada de dados*/
		validator.checking(ValidationRules.isValidQuestionRegistrationInput(market));
		validator.onErrorUse(Results.page()).of(MarketController.class).error();
		/* Executando logica de negocio */
		Category c = new Category();
		c.setId(market.getCategoryId());
		c = this.categoryService.find(c);
		market.setCategoryName(c.getName());
		market.setStatus(StatusType.opened);
		this.questionSessionHandler.setMarket(market);
		return market;
	}
	@Post
	@Path("/market/answers/add")
	public List<MarketAnswer> addAnswer(MarketAnswer marketAnswer) {
		/* Validando a entrada de dados*/
		validator.checking(ValidationRules.isValidQuestionAnwsersRegistrationInput(marketAnswer));
		validator.onErrorUse(Results.page()).of(MarketController.class).error();
		/* Executando logica de negocio */
		this.questionSessionHandler.addAnswer(marketAnswer);
		return this.questionSessionHandler.getMarketAnswers();
	}
	
	@Path("/market/answers/drop/{marketAnswer.tempId}")
	public List<MarketAnswer> dropAnswer(MarketAnswer marketAnswer) {
		this.questionSessionHandler.dropAnswer(marketAnswer);
		return this.questionSessionHandler.getMarketAnswers();
	}
	
	@Post
	@Path("/market/publish")
	public Market publish(String published) {
		Market session = this.questionSessionHandler.getMarket();
		if ((published==null)||(!published.trim().equals("on"))) {
			session.setPublished(0);
		} else {
			session.setPublished(1);
		}
		List<MarketAnswer> qas = this.questionSessionHandler.getMarketAnswers();
		this.questionSessionHandler.clean();
		return this.questionService.store(session,qas);
	}
	
	@Protect(roles="USER")
	@Path(priority = 2, value = "/market/{market.id}")
	public void view(Market market) {
		Market q = this.questionService.find(market);
		this.result.include("market", q);
		List<MarketAnswer> answers = this.questionService.findAnswersByMarket(market);
		this.result.include("answers", answers);
		this.result.include("answersMap", Util.toMap(answers));
		
		List<MarketComment> comments = this.questionService.findCommentsByMarket(market);
		this.result.include("comments", comments);
		this.result.include("commentsMap", Util.toMapComment(comments));
		
		MarketInfoVO mInfo = this.tradeService.getMarketUserTradeInfo(market,this.loginHandler.getUser());
		
		this.result.include("userTradeInfo",mInfo);
	}
	
	
	
}